package org.melanesia.beans;

import java.lang.reflect.Field;

import org.melanesia.beans.exceptions.PropertySetException;


class FieldGetter implements Getter {
    
    private Field field;
    
    FieldGetter(Field field) {
        this.field = field;
    }
    
    @Override
    public Object get(Object bean) {
        boolean accessible = field.isAccessible();
        
        try {
            if (!accessible) {
                BeanUtils.setAccessible(field, true);
            }
            
            return field.get(bean);
            
        } catch (Throwable t) {
            throw new PropertySetException("Cannot set value of field " + field.getName(), t);
        } finally {
            BeanUtils.setAccessible(field, accessible);
        }
    }

    @Override
    public Class<?> getReturnType() {
        return field.getType();
    }

}
